package com.frcdi.qms.web.controller.equipment;

import com.frcdi.qms.common.core.controller.BaseController;
import com.frcdi.qms.common.core.domain.AjaxResult;
import com.frcdi.qms.common.core.page.TableDataInfo;
import com.frcdi.qms.common.utils.StringUtils;
import com.frcdi.qms.common.utils.poi.ExcelUtil;
import com.frcdi.qms.equipment.domain.EquipmentCycleConfig;
import com.frcdi.qms.equipment.service.IEquipmentCycleConfigService;
import com.frcdi.qms.equipment.service.IEquipmentCycleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 设备生命周期配置Controller
 *
 * @author cell
 * @date 2023-12-12
 */
@Api(tags = "设备生命周期配置")
@RestController
@RequestMapping("/equipment/config")
public class EquipmentCycleConfigController extends BaseController
{
    @Autowired
    private IEquipmentCycleConfigService equipmentCycleConfigService;

    @Autowired
    private IEquipmentCycleService cycleService;

    /**
     * 查询设备生命周期配置列表
     */
    @ApiOperation("查询设备生命周期配置列表")
    @PreAuthorize("@ss.hasPermi('system:config:list')")
    @GetMapping("/list")
    public TableDataInfo list(EquipmentCycleConfig equipmentCycleConfig)
    {
        startPage();
        List<EquipmentCycleConfig> list = equipmentCycleConfigService.selectEquipmentCycleConfigList(equipmentCycleConfig);
        return getDataTable(list);
    }

    /**
     * 导出设备生命周期配置列表
     */
    @ApiOperation("导出设备生命周期配置列表")
    @PreAuthorize("@ss.hasPermi('system:config:export')")
    @PostMapping("/export")
    public void export(HttpServletResponse response, EquipmentCycleConfig equipmentCycleConfig)
    {
        List<EquipmentCycleConfig> list = equipmentCycleConfigService.selectEquipmentCycleConfigList(equipmentCycleConfig);
        ExcelUtil<EquipmentCycleConfig> util = new ExcelUtil<EquipmentCycleConfig>(EquipmentCycleConfig.class);
        util.exportExcel(response, list, "设备生命周期配置数据");
    }

    /**
     * 获取设备生命周期配置详细信息
     */
    @ApiOperation("获取设备生命周期配置详细信息")
    @PreAuthorize("@ss.hasPermi('system:config:query')")
    @GetMapping(value = "/{configId}")
    public AjaxResult getInfo(@PathVariable("configId") Long configId)
    {
        return success(equipmentCycleConfigService.selectEquipmentCycleConfigByConfigId(configId));
    }

    /**
     * 新增设备生命周期配置
     */
    @ApiOperation("新增设备生命周期配置")
    @PreAuthorize("@ss.hasPermi('system:config:add')")
    @PostMapping
    public AjaxResult add(@RequestBody EquipmentCycleConfig equipmentCycleConfig)
    {
        if (equipmentCycleConfig.getBeforeStatus().equals(equipmentCycleConfig.getEndStatus())){
            return error("前后状态相同，请修改!");
        }

        List<EquipmentCycleConfig> cycleConfigs = equipmentCycleConfigService.selectEquipmentCycleConfigByStatus(equipmentCycleConfig);
        if (cycleConfigs.size() > 0){
            return error("相同的数据已存在!");
        }

        equipmentCycleConfig.setCreateBy(getUsername());
        return toAjax(equipmentCycleConfigService.insertEquipmentCycleConfig(equipmentCycleConfig));
    }

    /**
     * 修改设备生命周期配置
     */
    @ApiOperation("修改设备生命周期配置")
    @PreAuthorize("@ss.hasPermi('system:config:edit')")
    @PutMapping
    public AjaxResult edit(@RequestBody EquipmentCycleConfig equipmentCycleConfig)
    {
        equipmentCycleConfig.setUpdateBy(getUsername());
        return toAjax(equipmentCycleConfigService.updateEquipmentCycleConfig(equipmentCycleConfig));
    }

    /**
     * 删除设备生命周期配置
     */
    @ApiOperation("删除设备生命周期配置")
    @PreAuthorize("@ss.hasPermi('system:config:remove')")
	@DeleteMapping("/{configIds}")
    public AjaxResult remove(@PathVariable Long[] configIds)
    {
        return toAjax(equipmentCycleConfigService.deleteEquipmentCycleConfigByConfigIds(configIds));
    }

    /**
     * 修改设备生命周期配置
     */
    @ApiOperation("验证是否可以修改")
    @PreAuthorize("@ss.hasPermi('system:config:edit')")
    @GetMapping("/validation")
    public AjaxResult validation(EquipmentCycleConfig equipmentCycleConfig)
    {
        Long equipmentId = equipmentCycleConfig.getEquipmentId();
        String beforeStatus = equipmentCycleConfig.getBeforeStatus();
        String endStatus = equipmentCycleConfig.getEndStatus();
        // 判空
        if (StringUtils.isNull(equipmentId) || StringUtils.isNull(beforeStatus) || StringUtils.isNull(endStatus)) {
            return error("检查你的填写信息");
        }
        // 查询是否存在记录
        List<EquipmentCycleConfig> cycleConfigs = equipmentCycleConfigService.selectEquipmentCycleConfigList(equipmentCycleConfig);
        AjaxResult ajax = AjaxResult.success();

        if (cycleService.countCycleById(equipmentId, endStatus) > 0 && cycleConfigs.size() > 0) {
            if (StringUtils.isNotNull(cycleConfigs.get(0).getMethodId())){
                ajax.put("validation", Boolean.FALSE);
                ajax.put(AjaxResult.DATA_TAG, cycleConfigs);
                return ajax;
            }else {
                return ajax.put("validation", Boolean.TRUE);
            }
        }else {
            return ajax.put("validation", Boolean.FALSE);
        }
    }
}
